package com.erp.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.bus.comm.DataAndView;
import com.erp.bus.comm.ResultCode;
import com.erp.sys.comm.TreeNode;
import com.erp.sys.Service.DeptService;
import com.erp.sys.pojo.Dept;
import com.erp.sys.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    // http://localhost:8080/dept/loadAllDept?page=1&limit=10
      @RequestMapping("loadAllDept")
  /*  title:
    address:
    remark:*/
    public DataAndView loadAllDept(DeptVo deptVo){

        IPage<Dept>  page  = new Page<Dept>(deptVo.getPage(),deptVo.getLimit());

        QueryWrapper<Dept> qw = new QueryWrapper<>();
        // title
          if(deptVo.getTitle() != null){
                qw.like("title",deptVo.getTitle());

                // like  eq   gt lt
          }

          if(deptVo.getAddress() != null){
              qw.like("address",deptVo.getAddress());
          }
          if(deptVo.getRemark() != null){
              qw.like("remark",deptVo.getRemark());
          }

        deptService.page(page,qw);

        return  new DataAndView(page.getRecords());

    }

    //  {
    //            "id": 1,
    //            "title": "行政部门",
    //            "icon": null,
    //            "href": null,
    //            "spread": false,
    //            "children": [],
    //            "checkArr": "0",
    //            "parentId": 0
    //        },

     // http://localhost:8080/dept/loadDeptManagerLeftTreeJson
    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataAndView loadDeptManagerLeftTreeJson(){

        // 查询所有部门信息
        List<Dept>  lists = deptService.list();

        List<TreeNode> davs = new ArrayList<>();

        for(Dept dept:lists){
            boolean spread = dept.getOpen() == 0?false:true;
            TreeNode tn =   new TreeNode(dept.getId(),dept.getTitle(),spread,"0",dept.getPid());
            davs.add(tn);
        }
        return new DataAndView(davs);

    }


    //添加
    // http://localhost:8080/dept/loadDeptMaxOrderNum
    // 获取最大值ordernum +1
    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){

        Integer maxOrderNum = null;
        QueryWrapper<Dept> qw = new  QueryWrapper();
        qw.orderByDesc("ordernum");

        List<Dept> lists = deptService.list(qw);
        maxOrderNum = lists.get(0).getOrdernum();

        Map map =  new HashMap<String,Object>();
        if(lists.size()>0){

            map.put("value",maxOrderNum +1);
        }else{

            map.put("value",1);
        }

        return map;
    }
   // http://localhost:8080/dept/addDept
    @RequestMapping("addDept")    // 既可以接受post   get
    public ResultCode addDept(DeptVo deptVo){
        deptVo.setId(null);
        ;try{
            boolean bool = deptService.save(deptVo);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else{
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_ADD;
        }
    }

    // 更新
    // http://localhost:8080/dept/loadDeptMaxOrderNum
    //http://localhost:8080/dept/updateDept


    // 删除  接口
    // http://localhost:8080/dept/checkDeptHasChildrenNode
    // http://localhost:8080/dept/delDept


}
